<?php

class Model_Forum_Group extends Forum_Db_Table
{
    protected $_name = 'forum_group';
    
    protected $_primary = 'id';


    /**
     * Get list of group_forum
     * 
     * @return array
     */
	public function getList($mode = 'enabled'){
		$query = "SELECT *
                  FROM {$this->_name}";

	    if ($mode != 'all') {
            $query .= " WHERE status = '{$mode}'";
        }
		
        return $this->getAdapter()->fetchAll($query);
	}
	
	/**
     * Get group_forum by id
     * 
     * @return array
     */
	public function getById($id){
		if(isset($id)){
			$query = "SELECT *
	                  FROM {$this->_name}
					  WHERE status = 'enabled' AND id = {$id}";
	
	        return $this->getAdapter()->fetchRow($query);			
		}
		return null;
	}
	
    /**
     * Add a new forum_group
     * 
     * @param array $data
     * @return bool
     */
    public function insert($data)
    {
        if (!isset($data['title']) || empty($data['title'])) {
            Forum_Message::getInstance()->addError('Вкажіть назву групи');
            return false;
        }
        
        $data['user_id'] = Model_User::getUserId();
        
        $this->getAdapter()->insert($this->_name, $data);
    	return true;
    }

    /**
     * Delete group rows by ids
     * 
     * @param mixed $id
     * @return bool
     */
    public function delete($ids)
    {
        if (!is_array($ids)) {
            $ids = array($ids);
        }
        
        $count = count($ids);
        $ids = implode(',', $ids);
        
        if ($this->getAdapter()->delete($this->_name, "id IN ({$ids})")) {
            Forum_Message::getInstance()->addSuccess(sprintf('%d %s успішно видалено', $count, (
                $count > 4 ? 'груп' : (
                    $count > 1 ? 'групи' : 'групу' //this is wrong, and i know this :/
                )
            )));
            return true;
        } else {
            Forum_Message::getInstance()->addError('Записи не були видалені. Спробуйте ще раз');
            return false;
        }
    }
	
    /**
     * Updates group row according to $bind param
     * 
     * @param mixed $ids
     * @param array $bind Column => Value pairs
     * @return bool
     */
    public function update($ids, $bind)
    {
        if (!is_array($ids)) {
            $ids = array($ids);
        }
        
        $count = count($ids);
        $ids = implode(',', $ids);
        
        if ($this->getAdapter()->update($this->_name, $bind, array("id IN ({$ids})"))) {
            Forum_Message::getInstance()->addSuccess(sprintf('%d %s успішно оновлено', $count, (
                $count > 4 ? 'груп' : (
                    $count > 1 ? 'групи' : 'групу' //this is wrong, and i know this :/
                )
            )));
            return true;
        } else {
            Forum_Message::getInstance()->addError('Записи не були оновлені. Спробуйте ще раз');
            return false;
        }
    }
}

?>